package com.kmust.dream.wanderingdream.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kmust.dream.wanderingdream.comm.pojo.ResultBean;
import com.kmust.dream.wanderingdream.product.entity.ProductCategory;
import com.kmust.dream.wanderingdream.product.service.ProductCategoryService;
import com.kmust.dream.wanderingdream.product.mapper.ProductCategoryMapper;
import com.kmust.dream.wanderingdream.product.utils.ReturnUtils;
import com.kmust.dream.wanderingdream.product.vo.ProductCategoryVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Service
@Transactional
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory>
        implements ProductCategoryService {


    @Override
    public ResultBean getAllCategory() {
        List<ProductCategory> list = this.list(
                new QueryWrapper<ProductCategory>()
        );
        return ReturnUtils.getList(list);
    }

    @Override
    public ResultBean getCategoryOne(Integer categoryId) {
        ProductCategory byId = this.getById(categoryId);
        return ReturnUtils.getOne(byId);
    }

    @Override
    public ResultBean getSonCategory(Integer parentCategory) {
        QueryWrapper<ProductCategory> queryWrapper = new QueryWrapper<ProductCategory>()
                .eq("category_parent_id", parentCategory)
                .eq("status", 1);
        List<ProductCategory> list = this.list(queryWrapper);
        return ReturnUtils.getList(list);
    }

    @Override
    public ResultBean getFirstCategory() {
        QueryWrapper<ProductCategory> queryWrapper = new QueryWrapper<ProductCategory>().eq("category_parent_id", 0);
        return ReturnUtils.getList(this.list(queryWrapper));
    }

    @Override
    public ResultBean addCategory(ProductCategory productCategory) {
        return ReturnUtils.add(this.save(productCategory));
    }

    @Override
    public ResultBean updateCategory(ProductCategory productCategory) {
        return ReturnUtils.update(this.updateById(productCategory));
    }

    @Override
    public ResultBean deleteCategory(Integer categoryId) {
        UpdateWrapper<ProductCategory> updateWrapper = new UpdateWrapper<ProductCategory>().eq("category_id", categoryId).set("status", false);
        return ReturnUtils.delete(this.update(updateWrapper));
    }

    @Override
    public ResultBean getAllCategoryFont() {
        List<ProductCategoryVO> productCategoryVOS = new ArrayList<>();
        ResultBean firstCategory = getFirstCategory();
        //抽象递归方法
        if (String.valueOf(firstCategory.getStatus()).startsWith("2")) {
            List<ProductCategory> firstCategoryPayloads = (List<ProductCategory>) firstCategory.getPayload();
            for (ProductCategory itemFirst : firstCategoryPayloads) {
                ProductCategoryVO child = getChild(itemFirst.getCategoryId());
                productCategoryVOS.add(child);
            }
        }
        return ReturnUtils.getList(productCategoryVOS);
    }


    public ProductCategoryVO getChild(Integer categoryId) {
        //根据顶级列表获取，当前节点
        ProductCategory byId = getById(categoryId);
        //创建节点，即为返回结果
        ProductCategoryVO res = new ProductCategoryVO(byId.getCategoryId(), byId.getCategoryTitle(),  byId.getStatus(),null);
        //寻找该节点所有的子节点
        QueryWrapper<ProductCategory> queryWrapper = new QueryWrapper<ProductCategory>()
                .eq("category_parent_id", categoryId);
        List<ProductCategory> list = list(queryWrapper);
        if (list == null || list.size() == 0) return res;
        //对子节点进行遍历
        List<ProductCategoryVO> children = new ArrayList<>();
        for (ProductCategory productCategory : list) {
            children.add(getChild(productCategory.getCategoryId()));
        }
        res.setChildren(children);
        return res;
    }
}




